﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Tracing;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Markup;

namespace Gl_mes.Tools.DataGirdViewExt
{
    public class DataGridViewCalendarColumn : DataGridViewColumn
    {
        private bool varValue = true;

        public bool ShowUpDown
        {
            get { return varValue; }
            set { varValue = value; }
        }

        public DataGridViewCalendarColumn() : base(new DataGridViewCalendarCell())
        {
            
        }

        public override DataGridViewCell CellTemplate
        {
            get { return base.CellTemplate; }
            set 
            {
                if (value != null && value.GetType().IsAssignableFrom(typeof(DataGridViewCalendarCell)))
                {
                    throw new InvalidOperationException("Mast be a DataGridViewCalendarCell");
                }
                base.CellTemplate = value;
            }
        }  
        
        public void InitializeComponent()
        {
            
        }
    }

    public class DataGridViewCalendarCell : DataGridViewTextBoxCell
    {
        public DataGridViewCalendarCell() : base()
        {
            
        }

        public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
        {
            base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
            CalendarEditingControl ctrl = DataGridView.EditingControl as CalendarEditingControl;
            if (this.Value != DBNull.Value)
            {
                ctrl.Value = Convert.ToDateTime(this.Value);
            }
            else
            {
                ctrl.Value = DateTime.Now;
            }
            ctrl.Format = DateTimePickerFormat.Custom;
            ctrl.CustomFormat = dataGridViewCellStyle.Format;
        }

        public override Type EditType
        {
            get { return typeof(CalendarEditingControl); }
        }

        public override Type ValueType
        {
            get { return typeof(DateTime); }
        }

        public override object DefaultNewRowValue
        { 
            get { return DateTime.Now; } 
        }
    }

    public class CalendarEditingControl : DateTimePicker, IDataGridViewEditingControl
    {
        DataGridView dgv;
        private bool valueChange = false;
        int rowIndex;

        public CalendarEditingControl()
        {

        }

        public object EditingControlFormattedValue
        {
            get { return this.Value.ToString(); }
            set 
            {
                string newValue = value as string;
                if (newValue != null)
                {
                    this.Value = DateTime.Parse(newValue);
                }
            }
        }

        public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts ctx)
        {
            return EditingControlFormattedValue;
        }

        public void ApplyCellStyleToEditingControl (DataGridViewCellStyle dataGridViewCellStyle)
        {
            this.Font = dataGridViewCellStyle.Font;
            this.CalendarForeColor = dataGridViewCellStyle.ForeColor;
            this.CalendarMonthBackground = dataGridViewCellStyle.BackColor;
        }

        public int EditingControlRowIndex
        {
            get { return rowIndex; }
            set { rowIndex = value; }
        }

        public bool EditingControlWantsInputKey(Keys key, bool dataGridViewWantsInputKey)
        {
            switch (key & Keys.KeyCode)
            {
                case Keys.Left:
                case Keys.Up: 
                case Keys.Down:
                case Keys.Right:
                case Keys.Home:
                case Keys.End:
                case Keys.PageDown: 
                case Keys.PageUp:
                    return true;
                default:
                    return false;
            }
        }

        public void PrepareEditingControlForEdit(bool selectAll)
        {
            
        }

        public bool RepositionEditingControlOnValueChange
        {
            get { return false; }
        }

        public DataGridView EditingControlDataGridView
        {
            get { return dgv; }
            set { dgv = value; }
        }

        public bool EditingControlValueChanged
        {
            get { return valueChange; }
            set { valueChange = value; }
        }

        public Cursor EditingPanelCursor
        {
            get { return base.Cursor; }
        }

        protected override void OnValueChanged(EventArgs eventargs)
        {
            valueChange = true;
            this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
            base.OnValueChanged(eventargs);
        }
    }
}
